package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.CourseCollectService;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.service.LearnCourseService;
import com.wxxymaker.edu.service.SysCountService;
import com.wxxymaker.edu.utils.TokenUtils;

/**
 * @AUTHOR soft
 * @DATE 2018/9/6 17:54
 * @DESCRIBE 课程设置相关操作接口
 */
@TeacherRoles
@Path(value = "/course/set", restful = true)
public class CourseSetHandlerApi {

    @Inject
    private CourseService courseService;

    @Inject
    private LearnCourseService learnCourseService;

    @Inject
    private CourseCollectService courseCollectService;

    @Inject
    private SysCountService sysCountService;

/*################ set 接口 ###############*/
    /**
     * 初始化添加课程信息 主要用于建立基本的课程信息 确定课程id
     * @param name 课程名
     */
    @PostRoute("/newCourse")
    public RestResponse add_(@Param String name) {
        Course course = new Course();
        course.setName(name);
        course.setType(1);
        course.setCover("/static/img/courseset.png");
        course.setUserId(TokenUtils.currentUser().getId());
        Course add = courseService.add(course);
        if (add != null) {
            return RestResponse.ok(add);
        }
        return RestResponse.fail();
    }

    /**
     * 课程修改
     * @param kid 课程id
     * @param course 课程实体对象
     */
    @PostRoute("/:kid/change")
    public RestResponse change(@PathParam int kid,
                               Course course) {
        course.setId(kid);
        if (courseService.change(course)) {
            return RestResponse.ok("修改成功！");
        }
        return RestResponse.fail();
    }

    /**
     * 发布课程
     * @param kid 课程id
     */
    @PostRoute("/:kid/publish")
    public RestResponse publish(@PathParam int kid) {
        Course course = new Course();
        course.setId(kid);
        course.setStatus(1);
        if (courseService.change(course)) {
            return RestResponse.ok("发布成功！");
        }
        return RestResponse.fail();
    }

    /**
     * 取消发布课程
     * @param kid 课程id
     */
    @PostRoute("/:kid/unpublish")
    public RestResponse unPublish(@PathParam int kid) {
        Course course = new Course();
        course.setId(kid);
        course.setStatus(0);
        if (courseService.change(course)) {
            learnCourseService.unJoins(kid);
            courseCollectService.cancels(kid);
            return RestResponse.ok("取消发布成功！");
        }
        return RestResponse.fail();
    }


    @PostRoute("/:kid/del")
    public RestResponse delete(@PathParam Integer kid) {
        Course course = courseService.delete(kid);
        if (course != null) {
            sysCountService.clearCourse(kid);
            return RestResponse.ok("删除成功");
        }
        return RestResponse.fail();
    }
}
